package org.example.lostandfound.controller;

import org.example.lostandfound.dao.LostItemDao;
import org.example.lostandfound.model.LostItem;
import org.example.lostandfound.model.LostItemType;
import org.example.lostandfound.model.User;
import org.example.lostandfound.service.LostItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
@RestController
@RequestMapping("/api/lost-items")
public class LostItemController {

    @Autowired
    private LostItemService lostItemService;

    @GetMapping
   public ResponseEntity<List<LostItem>> getAllLostItems(HttpSession session) {
        User user = (User) session.getAttribute("user");
        if(user == null) {
            return ResponseEntity.ok().body(lostItemService.findAllLostItems());
        }else {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
        }
    }

    @GetMapping("/{id}")
    public LostItem getLostItemById(@PathVariable Long id) {
        return lostItemService.findLostItemById(id);
    }

    @PostMapping
    public void insertLostItem(@RequestBody LostItem lostItem) {
        lostItemService.insertLostItem(lostItem);
    }

    @PutMapping("/{id}")
    public void updateLostItem(@PathVariable Long id, @RequestBody LostItem lostItem) {
        lostItem.setLostItemId(id);
        lostItemService.updateLostItem(lostItem);
    }

    @DeleteMapping("/{id}")
    public void deleteLostItem(@PathVariable Long id) {
        lostItemService.deleteLostItem(id);
    }

    @GetMapping
    public List<LostItem> findLostItemsByFound(@PathVariable boolean found) {
        return lostItemService.findLostItemsByFound(found);
    }

    @PostMapping("/{id}/found")
    public ResponseEntity<Void> findLostItemById(@PathVariable Long id) {
        lostItemService.findLostItemById(id);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/type/{type}")
    public ResponseEntity<List<LostItem>> findLostItemsByType(@PathVariable LostItemType type) {
        List<LostItem> lostItems =lostItemService.findLostItemsByType(type);
        return ResponseEntity.ok().body(lostItems);

    }

    @PostMapping("/upload")
    public ResponseEntity<?> uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
        if(!file.isEmpty()) {
            String originalfileName = file.getOriginalFilename();
            String uploadDir = "/path/to/upload/";

            ResourceLoader  loader= new DefaultResourceLoader();
            Resource resource = loader.getResource("file:"+ uploadDir + originalfileName);

            if(!resource.exists()) {
                return ResponseEntity.ok()
                        .header(HttpHeaders.CONTENT_DISPOSITION,"atttachment; filename=\"" + originalfileName +"\"" )
                        .contentType(MediaType.APPLICATION_OCTET_STREAM)
                        .body(resource);
            }else{
                return ResponseEntity.badRequest().build();
            }
        }else {
            return ResponseEntity.badRequest().build();
        }
    }

}
